package com.gongpingjialoan.widget;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.car.SecondDetailFragment;
import com.gongpingjialoan.api.Piont;
import com.gongpingjialoan.data.UserManager;
import com.gongpingjialoan.global.GPJApplication;
import com.gongpingjialoan.manage.PointRecordManage;
import com.gongpingjialoan.utility.Utils;

/**
 * Created by Administrator on 2016/5/23.
 */
public class OrderSeeCarPopup extends PopupWindow implements View.OnClickListener{

    private Context mContext;

    private SecondDetailFragment mFragment;

    private View mView , main , main1;

    private EditText mEditText;

    private ImageView close;

    private TextView callTextView;

    private Button mButton;

    private CheckBox mCheckBox;

    public OrderSeeCarPopup(Fragment fragment) {
        super(fragment.getActivity());
        mFragment = (SecondDetailFragment) fragment;
        this.mContext = fragment.getActivity();
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mView = inflater.inflate(R.layout.order_see_car , null);

        this.setContentView(mView);

        mCheckBox = (CheckBox) mView.findViewById(R.id.checkbox);
        mCheckBox.setChecked(false);
        mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    PointRecordManage.getInstance().addPoint(mContext, Piont.SourceAppointmentDetectionClick);
                }
            }
        });
        main = mView.findViewById(R.id.main);
        main1 = mView.findViewById(R.id.main1);
        callTextView = (TextView) mView.findViewById(R.id.call);
        close = (ImageView) mView.findViewById(R.id.close);
        mButton = (Button) mView.findViewById(R.id.sure_button);
        mEditText = (EditText) mView.findViewById(R.id.phone);

        if (GPJApplication.getInstance().isLogin()) {
            UserManager mUserManager = new UserManager(fragment.getActivity());
            mUserManager = mUserManager.LoadUserInfo();
            mEditText.setText(mUserManager.getPhone());
        }

        close.setOnClickListener(this);
        mButton.setOnClickListener(this);
        main.setOnClickListener(this);
        callTextView.setOnClickListener(this);
        main1.setOnClickListener(this);
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable cd = new ColorDrawable(0x000000);
        this.setBackgroundDrawable(cd);
    }

    @Override
    public void onClick(View v) {
        if(v == close || v == main){
            dismiss();
        }else if(v == mButton){
            PointRecordManage.getInstance().addPoint(mContext, Piont.SourceAppointmentCommit);
            String num = mEditText.getText().toString();
            if(num.length() < 11) {
                PointRecordManage.getInstance().addPoint(mContext, Piont.SourceAppointmentAlert);
                Toast.makeText(mContext, "请输入正确的手机号码", Toast.LENGTH_SHORT).show();
                return;
            }
            dismiss();
            mFragment.orderCar(num , mCheckBox.isChecked() , true);
        }else if(v == callTextView){
            Utils.callKefu(mContext);
        }
    }
}
